<template>
  <div>
    <i class="el-icon-back" @click="goBack()">返回</i>
    <el-input
      placeholder="请输入文章标题"
      v-model="article.title"
      style="margin-bottom:4px;width:70%">
    </el-input>
    <el-button round type="primary" @click="publish(article)">发布文章</el-button>
    <el-input
      placeholder="请输入文章价格"
      v-model="article.price"
      style="margin-left:52px;margin-bottom:20px;width:20%;margin-top: 5px;">
    </el-input>
    <el-select v-model="article.tid" placeholder="请选择文章类别">
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <mavon-editor style="height:550px" @save="save(article)" v-model="article.content"/>
  </div>
</template>
 
<script>
import { saveArticle } from "@/api/article/WriteArticle.js";
export default {
  name: "WriteArticle",
  
  data() {
    return {
      article:{
        content:'',
        title:"",
        tid:'',
        price:0
      },
      value:'',
      options: [{
          value: '1323112403789201410',
          label: 'java'
        }, {
          value: '1323139780862881793',
          label: 'html/css'
        }, {
          value: '1323138893541085186',
          label: 'python'
        }, {
          value: '1323138705657241602',
          label: 'go'
        }, {
          value: '1323139552805949441',
          label: 'javascript'
        }, {
          value: '1323139127977521154',
          label: 'php'
        }
        
      ]
    }
  },
  created(){
    if(this.$route.query.article){
      this.article=this.$route.query.article
    }
  },
  methods:{
    //返回
    goBack() {
      this.$router.go(-1);
    },
    save(article){
      //内容为空则不保存
      if(article.content!==''){
          //alert(article)
          saveArticle(article).then(response=>{
            this.$message({
              message: '保存成功',
              type: 'success'
            });
          })
      }
       
    },
    //发布文章
    publish(article){
      //内容为空则不保存
      if(article.content!==''){
          //alert(article)
          saveArticle(article).then(response=>{
            this.$message({
              message: '发布成功，待审核',
              type: 'success'
            });
            //跳转到我的文章页面
            this.$router.push({
                path: '/myArticle'
            });
          })
      }
       
    }
  }
};
</script>
 
<style scoped>
.article-price{
  width:200px;
  height:50px;
}
</style>